import { v4 as uuidv4 } from "uuid";

let users = [
  {
    id: "1",
    name: "张三",
    gender: "男",
    age: 25,
    email: "zhang@example.com",
    phone: "13800138000",
  },
  {
    id: "2",
    name: "李四",
    gender: "女",
    age: 30,
    email: "li@example.com",
    phone: "13900139000",
  },
];

export const mockUserApi = {
  getUsers: () =>
    new Promise((resolve) => setTimeout(() => resolve({ data: users }), 500)),

  createUser: (user) =>
    new Promise((resolve) => {
      setTimeout(() => {
        const newUser = { ...user, id: uuidv4() };
        users = [...users, newUser];
        resolve({ data: newUser });
      }, 500);
    }),

  updateUser: (id, user) =>
    new Promise((resolve) => {
      setTimeout(() => {
        users = users.map((u) => (u.id === id ? { ...u, ...user } : u));
        resolve({ data: { id, ...user } });
      }, 500);
    }),

  deleteUser: (id) =>
    new Promise((resolve) => {
      setTimeout(() => {
        users = users.filter((u) => u.id !== id);
        resolve({ data: { id } });
      }, 500);
    }),
};
